package com.rjwh.dingdong.client.activity.community;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.orangegangsters.github.swipyrefreshlayout.library.SwipyRefreshLayout;
import com.orangegangsters.github.swipyrefreshlayout.library.SwipyRefreshLayoutDirection;
import com.rjwh.dingdong.client.R;
import com.rjwh.dingdong.client.activity.BaseActivity;
import com.rjwh.dingdong.client.adapter.CommunityPostAdapter;
import com.rjwh.dingdong.client.application.MyApplication;
import com.rjwh.dingdong.client.bean.localbean.CommunityPostListBean;
import com.rjwh.dingdong.client.bean.localbean.TopicData;
import com.rjwh.dingdong.client.constant.LocalConstant;
import com.rjwh.dingdong.client.mvp.presenter.impl.CommunityPostDigestPresenter;
import com.rjwh.dingdong.client.mvp.view.CommunityPostView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author Kay
 * @ClassName CommunityPostDigestActivity
 * @Description 社区精华帖子列表
 * @date 2016-8-10
 * @history 2016-8-10 author: description:
 */
public class CommunityPostDigestActivity extends BaseActivity implements CommunityPostView {
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.community_post_digest_listview)
    ListView communityPostDigestListview;
    @BindView(R.id.community_post_digest_mPullRefreshView)
    SwipyRefreshLayout communityPostDigestMPullRefreshView;
    private int pageNumber = 0;
    private CommunityPostAdapter adapter;
    private CommunityPostDigestPresenter presenter;
    private String modularid;


    public static void startActivity(Context context, String modularid) {
        Intent intent = new Intent(context, CommunityPostDigestActivity.class);
        intent.putExtra("modularid", modularid);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.community_post_digest_layout);
        ButterKnife.bind(this);
        parseIntent();
        initView();
        presenter = new CommunityPostDigestPresenter(this, this);
        communityPostDigestMPullRefreshView.doPullRefreshing();
    }

    private void parseIntent() {
        Intent intent = getIntent();
        if (intent != null) {
            modularid = intent.getStringExtra("modularid");
        }
    }

    private void initView() {
        initToolbarHelper();
        communityPostDigestMPullRefreshView.setOnRefreshListener(new SwipyRefreshLayout.OnRefreshListener() {

            @Override
            public void onRefresh(SwipyRefreshLayoutDirection direction) {
                switch (direction) {
                    case TOP:
                        pageNumber = 0;
                        doGetDigestTopicList(true, SwipyRefreshLayoutDirection.TOP);
                        break;

                    case BOTTOM:
                        pageNumber++;
                        doGetDigestTopicList(false, SwipyRefreshLayoutDirection.BOTTOM);
                        break;

                    default:
                        break;
                }
            }
        });
        adapter = new CommunityPostAdapter(this);
        communityPostDigestListview.setAdapter(adapter);
        communityPostDigestListview.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                CommunityPostListBean item = (CommunityPostListBean) parent.getAdapter().getItem(position);
                if (item == null) {
                    return;
                }
                int type = item.getType();
                if (type == 1) {
                    TopicData topicItem = (TopicData) item.getObject();

                    String filepath = topicItem.getFilepath();
                    Intent intent = new Intent(CommunityPostDigestActivity.this, CommunityPostDetailActivity.class);
                    if (topicItem.getTopicid() != null) {
                        intent.putExtra("topicid", topicItem.getTopicid());
                    }
                    if (filepath != null) {
                        String url = filepath + "?userid=" + MyApplication.spUtil.getStrPreferenceByParamName(MyApplication.app, LocalConstant.SP_USERID) + "&platform=ANDROID" + "&version=" + MyApplication.versionCode + "&udid=" + MyApplication.deviceId + "&babyid=" + MyApplication.spUtil.getStrPreferenceByParamName(CommunityPostDigestActivity.this, LocalConstant.SP_BABYID) + "&token=" + MyApplication.spUtil.getStrPreferenceByParamName(MyApplication.app, LocalConstant.SP_USER_TOKEN);
                        intent.putExtra("url", url);
                    }
                    startActivity(intent);
                }
            }
        });

    }

    private void doGetDigestTopicList(boolean isRefresh, SwipyRefreshLayoutDirection direction) {
        if (modularid != null) {
            presenter.doGetDigestTopicList(modularid, pageNumber, isRefresh, direction);
        }
    }

    protected void initToolbarHelper() {
        this.setSupportActionBar(this.toolbar);
        assert getSupportActionBar() != null;
        getSupportActionBar().setTitle("精华贴文");
        getSupportActionBar().setHomeButtonEnabled(true); //设置返回键可用
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    @Override
    public void setDirection(SwipyRefreshLayoutDirection direction) {
        communityPostDigestMPullRefreshView.setDirection(direction);
    }

    @Override
    public void setRefreshing(boolean refreshing) {
        communityPostDigestMPullRefreshView.setRefreshing(refreshing);
    }

    @Override
    public void loadData(boolean isRefresh, List<CommunityPostListBean> data) {
        if (isRefresh) {
            adapter.clear();
        }
        adapter.appendToList(data);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.detachView();
    }
}
